;
(function (win, doc) {
    // 更新页面用requestAnimationFrame替代setTimeout
    win.requestAnimationFrame = win.requestAnimationFrame
        || win.mozRequestAnimationFrame
        || win.webkitRequestAnimationFrame
        || win.msRequestAnimationFrame;

    function Game() {
        this.canvas = $('#canvas');
        this.ctx = this.canvas.getContext('2d');
    }

    Game.prototype = {
        start() {
            var code = $('#codeBox').value;
            eval(code);
            this.run();
        },
        paint() {
            var width = this.canvas.width;
            var height = this.canvas.height;
            var x = getRandom(0, width);
            var y = getRandom(0, height);
            fillRoundRect(this.ctx, x, y, 20, 20, 10, '#f59563');
        },
        run() {
            this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
            this.paint();
            requestAnimationFrame(this.run.bind(this));
        }
    }

    win.onload = function () {
        win.game = new Game;
        var runBtn = $('#runBtn');
        runBtn.onclick = function () {
            game.start();
        }
    }
})(window, document);